package com.jjc.android.dialog

import android.content.Context
import android.view.View
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.jjc.android.R
import com.jjc.android.util.showImage
import com.lxj.easyadapter.EasyAdapter
import com.lxj.easyadapter.MultiItemTypeAdapter
import com.lxj.easyadapter.ViewHolder
import com.lxj.xpopup.core.AttachPopupView
import com.lxj.xpopup.interfaces.OnSelectListener
import com.lxj.xpopup.util.XPopupUtils
import com.lxj.xpopup.widget.VerticalRecyclerView
import kotlin.math.roundToInt

class CustomAttachListPopupView(context: Context) : AttachPopupView(context) {
    var recyclerView: VerticalRecyclerView? = null
    private var bindLayoutId = 0
    private var bindItemLayoutId = 0
    private lateinit var data: List<String>
    private var iconIds: List<Int>? = null
    private var iconUrls: List<String>? = null
    private var selectListener: OnSelectListener? = null

    fun bindLayout(layoutId: Int): CustomAttachListPopupView {
        bindLayoutId = layoutId
        return this
    }

    fun bindItemLayout(itemLayoutId: Int): CustomAttachListPopupView {
        bindItemLayoutId = itemLayoutId
        return this
    }

    override fun getMaxHeight(): Int {
        return (XPopupUtils.getScreenHeight(context)*0.4f).roundToInt()
    }

    override fun getImplLayoutId(): Int {
        return if (bindLayoutId == 0) R.layout.dialog_attach_impl_list else bindLayoutId
    }

    override fun initPopupContent() {
        super.initPopupContent()
        recyclerView = findViewById<View>(R.id.recyclerView) as VerticalRecyclerView
        recyclerView!!.setupDivider(false)
        val adapter: EasyAdapter<String> = object : EasyAdapter<String>(data.toList(),
            if (bindItemLayoutId == 0) R.layout._xpopup_adapter_text else bindItemLayoutId
        ) {
            protected override fun bind(holder: ViewHolder, s: String, position: Int) {
                holder.setText(R.id.tv_text, s)
                val iv_image:ImageView = holder.getView(R.id.iv_image)
                if (iconIds != null && iconIds!!.size > position) {
                    iv_image.visibility = View.VISIBLE
                    iv_image.setBackgroundResource(iconIds!![position])
                } else if (iconUrls != null && iconUrls!!.size > position) {
                    iv_image.showImage(iconUrls!![position])
                    iv_image.visibility = View.VISIBLE
                } else {
                    iv_image.visibility = View.GONE
                }
            }
        }
        adapter.setOnItemClickListener(object : MultiItemTypeAdapter.SimpleOnItemClickListener() {
            override fun onItemClick(view: View, holder: RecyclerView.ViewHolder, position: Int) {
                if (selectListener != null) {
                    selectListener!!.onSelect(position, adapter.data[position])
                }
                if (popupInfo.autoDismiss) {
                    dismiss()
                }
            }
        })
        recyclerView!!.adapter = adapter
    }

    fun setStringData(data: List<String>, iconIds: List<Int>?): CustomAttachListPopupView {
        this.data = data
        this.iconIds = iconIds
        return this
    }

    fun setStringDataUrl(data: List<String>, iconUrls: List<String>?): CustomAttachListPopupView {
        this.data = data
        this.iconUrls = iconUrls
        return this
    }

    fun setOffsetXAndY(offsetX: Int, offsetY: Int): CustomAttachListPopupView {
        defaultOffsetX += offsetX
        defaultOffsetY += offsetY
        return this
    }

    fun setOnSelectListener(selectListener: OnSelectListener?): CustomAttachListPopupView {
        this.selectListener = selectListener
        return this
    }
}